package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCode;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.entity.BasicType;
import cn.stylefeng.guns.modular.basic.model.params.BasicCodeParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicTypeGsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicTypeParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCodeResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicTypeGsResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicTypeResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicTypeGsService;
import cn.stylefeng.guns.modular.basic.service.BasicTypeService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.params.SysCompanyParam;
import cn.stylefeng.guns.sys.modular.system.model.result.SysCompanyResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 通用编码控制器
 *
 * @author 
 * @Date 2024-01-05 16:41:53
 */
@Controller
@RequestMapping("/basicType")
public class BasicTypeController extends BaseController {

    private String PREFIX = "/basic/basicType";

    @Autowired
    private BasicTypeService basicTypeService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;
    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2024-01-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicType.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2024-01-05
     */

    @Autowired
    private BasicTypeGsService basicTypeGsService;
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        BasicTypeGsParam basicTypeGsParam = new BasicTypeGsParam();
        basicTypeGsParam.setDeliverId(user.getDeliverId());
        List<BasicTypeGsResult> ztlist = basicTypeGsService.list2(basicTypeGsParam);
        model.addAttribute("ztlist",ztlist);
        return PREFIX + "/basicType.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2024-01-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/basicType_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 
     * @Date 2024-01-05
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicTypeParam basicTypeParam) {
        User user = userService.getcurrentUser();
        basicTypeParam.setDeliverId(user.getDeliverId());
        basicTypeParam.setCreateUser(user.getName());

        if (ToolUtil.isEmpty(basicTypeParam.getPid()) || basicTypeParam.getPid().equals(0L)) {
            basicTypeParam.setPid(0L);
            basicTypeParam.setPids("[0],");
        } else {
            Long pid = basicTypeParam.getPid();
            BasicType temp = basicTypeService.getById(pid);
            String pids = temp.getPids();
            basicTypeParam.setPid(pid);
            basicTypeParam.setPids(pids + "[" + pid + "],");
        }

        this.basicTypeService.add(basicTypeParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2024-01-05
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicTypeParam basicTypeParam) {
        basicTypeParam.setUpdateUser(userService.getcurrentUser().getName());

        if (ToolUtil.isEmpty(basicTypeParam.getPid()) || basicTypeParam.getPid().equals(0L)) {
            basicTypeParam.setPid(0L);
            basicTypeParam.setPids("[0],");
        } else {
            Long pid = basicTypeParam.getPid();
            BasicType temp = basicTypeService.getById(pid);
            String pids = temp.getPids();
            basicTypeParam.setPid(pid);
            basicTypeParam.setPids(pids + "[" + pid + "],");
        }
        this.basicTypeService.update(basicTypeParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2024-01-05
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicTypeParam basicTypeParam) {
        List<BasicTypeResult> list = basicTypeService.likePids(basicTypeParam.getId());
        for (BasicTypeResult r : list) {
            BasicTypeParam p1 = new BasicTypeParam();
            p1.setId(r.getId());
            basicTypeService.delete(p1);
        }
        this.basicTypeService.delete(basicTypeParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2024-01-05
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicTypeParam basicTypeParam) {
        BasicType detail = this.basicTypeService.getById(basicTypeParam.getId());
        BasicTypeResult basicTypeResult = new BasicTypeResult();
        ToolUtil.copyProperties(detail, basicTypeResult);

        if(basicTypeResult.getPid().equals(0l)) {
            basicTypeResult.setPName("顶级");
        } else {
            BasicType detail2 = this.basicTypeService.getById(basicTypeResult.getPid());
            basicTypeResult.setPName(detail2.getCodeName());
        }
        return ResponseData.success(basicTypeResult);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2024-01-05
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicTypeParam basicTypeParam,String condition ) {
        return this.basicTypeService.findPageBySpec(basicTypeParam,condition);
    }

    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree(BasicTypeParam basicTypeParam) {
        basicTypeParam.setDeliverId(userService.getDeliverId());
        List<ZTreeNode> tree = this.basicTypeService.tree(basicTypeParam);
        return tree;
    }

}


